{% extends 'base.html' %}

{% block title %}模型库管理{% endblock %}

{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
    <h1>模型库管理</h1>
    <a href="{{ url_for('model.add_model') }}" class="btn btn-primary">添加模型</a>
</div>

<div class="card mb-4">
    <div class="card-body">
        <form method="get" action="{{ url_for('model.model_list') }}" class="d-flex flex-wrap gap-3">
            <input type="text" name="search" class="form-control me-2" placeholder="搜索排放源或因子名称..." value="{{ search_query }}">
            <select name="category_id" class="form-select" onchange="this.form.submit()">
                <option value="">所有模型分类</option>
                {% for category in categories %}
                    <option value="{{ category.id }}" {% if selected_category == category.id|string %}selected{% endif %}>
                        {{ '　' * (category.level - 1) }}|-- {{ category.name }}
                    </option>
                {% endfor %}
            </select>
            <button type="submit" class="btn btn-outline-primary">搜索</button>
            {% if search_query or selected_category %}
                <a href="{{ url_for('model.model_list') }}" class="btn btn-outline-secondary">清除</a>
            {% endif %}
        </form>
    </div>
</div>

<div class="card">
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-striped table-hover">
                <thead>
                    <tr>
                        <th>排放源</th>
                        <th>模型分类</th>  <!-- 新增列 -->
                        <th>说明</th>
                        <th>数据来源</th>
                        <th>排放源单位</th>
                        <th>碳排放因子</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    {% for model in models %}
                    <tr>
                        <td>{{ model.emission_source }}</td>
                        <td>{{ model.model_category_path|default('未分类') }}</td>  <!-- 显示模型分类 -->
                        <td>{{ model.description }}</td>
                        <td>{{ model.data_source }}</td>
                        <td>{{ model.emission_unit }}</td>
                        <td>
                            <a href="{{ url_for('factor.edit_factor', id=model.factor_id) }}" target="_blank">
                                {{ model.factor_name }} ({{ model.factor_category_path|default('未分类') }})
                            </a>
                        </td>
                        <td>
                            <div class="btn-group btn-group-sm">
                                <a href="{{ url_for('model.edit_model', id=model.id) }}" class="btn btn-outline-primary">编辑</a>
                                <button type="button" class="btn btn-outline-danger" data-bs-toggle="modal" data-bs-target="#deleteModal{{ model.id }}">删除</button>
                            </div>
                            <!-- 删除确认模态框 -->
                            <div class="modal fade" id="deleteModal{{ model.id }}" tabindex="-1" aria-hidden="true">
                                <div class="modal-dialog">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <h5 class="modal-title">确认删除</h5>
                                            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                        </div>
                                        <div class="modal-body">
                                            <p>确定要删除模型 <strong>{{ model.emission_source }}</strong> 吗？此操作不可撤销。</p>
                                        </div>
                                        <div class="modal-footer">
                                            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                                            <form action="{{ url_for('model.delete_model', id=model.id) }}" method="post">
                                                <button type="submit" class="btn btn-danger">删除</button>
                                            </form>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </td>
                    </tr>
                    {% else %}
                    <tr>
                        <td colspan="7" class="text-center">暂无模型数据</td>  <!-- 列数从6改为7 -->
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
</div>
{% endblock %}